package com.gobang.gobangboot.service.impl;

import com.gobang.gobangboot.entity.User.User;
import com.gobang.gobangboot.entity.User.UserRegisterDTO;
import com.gobang.gobangboot.entity.User.UserVO;
import com.gobang.gobangboot.mapper.UserMapper;
import com.gobang.gobangboot.service.UserService;
import com.gobang.gobangboot.utils.BaseContext;
import com.gobang.gobangboot.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final String CODE_KEY = "USER:CODE:";

    @Override
    public void register(UserRegisterDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        String rePassword = userDTO.getRePassword();
        String phone = userDTO.getPhone();

        User user = userMapper.findByPhone(phone);
        if (user != null){
            throw new RuntimeException("此手机号已被注册！");
        }

        if (!password.equals(rePassword)){
            throw new RuntimeException("密码和重复密码不一致！");
        }
//        System.out.println(user);
        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setAvatar("https://q4.itc.cn/q_70/images03/20240405/39ec09deda3a41d79e03897b0fdf68a0.jpeg");
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public String login(UserRegisterDTO userDTO) {
        User user = userMapper.findByUsername(userDTO.getPhone());
        if (user == null){
            throw new RuntimeException("此用户不存在！");
        }

        if (!user.getPassword().equals(userDTO.getPassword())){
            throw new RuntimeException("密码错误！");
        }
        String token = getToken(user);
        return token;
    }

    @Override
    public User getInfo() {
        System.out.println("getInfo id:"+BaseContext.getCurrentId());
        User user = userMapper.findById(BaseContext.getCurrentId());
//        个签设默认值
        if (user.getIntro() == null || user.getIntro().equals("")){
            user.setIntro("这个家伙很懒，什么都没有留下");
        }
//        地址设默认值
        if (user.getArea() == null || user.getArea().equals("")){
            user.setArea("暂未定位");
        }
//        对局设默认值
        if (user.getTotalCount() == null){
            user.setTotalCount(0);
        }
        if (user.getWinCount() == null){
            user.setWinCount(0);
        }
//        电话设默认值
        if (user.getPhone() == null || user.getPhone().equals("")){
            user.setPhone("暂无号码");
        }
        User vo = new User();
        BeanUtils.copyProperties(user, vo);
        return vo;
    }

    @Override
    public UserVO getEnemy(String enemyName) {
        User user = userMapper.findByUsername(enemyName);
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(user, vo);
        return vo;
    }

//    更新用户信息
    @Override
    public void updateUserInfo(User user) {
        Long userId = BaseContext.getCurrentId();
        user.setId(userId);
        userMapper.updateById(user);
    }

    @Override
    public void updateAvatar(String urlPath) {
        Long id = BaseContext.getCurrentId();
        System.out.println("updateAvatar id:"+id);
        userMapper.updateAvatar(urlPath,id);
    }

    @Override
    public String loginByCode(UserRegisterDTO dto) {
        String KEY = CODE_KEY + dto.getPhone();
        String code = redisTemplate.opsForValue().get(KEY);
        if(code == null){
            throw new RuntimeException("你输的什么瘠薄手机号，没有找到这验证码啊？");
        }

        if (!code.equals(dto.getCode())){
            throw new RuntimeException("验证码错误咯，兄弟！");
        }

        User user = userMapper.findByPhone(dto.getPhone());

        if (user==null){
            throw new RuntimeException("手机号不存在！");
        }

        System.out.println(user);

        String token = getToken(user);
        System.out.println("token: " + token);
        redisTemplate.delete(KEY);

        return token;
    }

    public String getToken(User user){
        Map<String, Object> cliams = new HashMap<>();
        cliams.put("id", user.getId());
        cliams.put("username", user.getUsername());
        String token = JwtUtil.getToken(cliams);
        return token;
    }
}
